package com.enlighten.featureExtractor.radiusFeatureStrategy;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import com.enlighten.featureExtractor.IRadiusFeatureStrategy;
import com.enlighten.featureExtractor.coinFeature.RadiusBasedCoinFeature;

public class SimpleRadiusFeatureStrategy implements IRadiusFeatureStrategy<RadiusBasedCoinFeature> {

	private int radius;
	private int resolution;
	private double currentRadius;
	private int iteration;
	private Map<Integer,RadiusBasedCoinFeature> radiusFeatureMap = new HashMap<Integer, RadiusBasedCoinFeature>();

	public SimpleRadiusFeatureStrategy(int radius, int resolution) {
		currentRadius = 0;
		iteration = 0;
		this.resolution = resolution;
		this.radius = radius;
	}

	@Override
	public boolean hasNext() {
		iteration++;
		currentRadius = iteration * ((double)radius)/((double)resolution);
		return Math.floor(currentRadius) < radius;
	}

	@Override
	public RadiusBasedCoinFeature next() {
		RadiusBasedCoinFeature radiusBasedCoinFeature = new RadiusBasedCoinFeature(iteration);
		radiusFeatureMap.put((int) Math.floor(currentRadius), radiusBasedCoinFeature);
		return radiusBasedCoinFeature;
	}

	@Override
	public RadiusBasedCoinFeature getFeatureFor(int squaredDistanceFromCenter) {
		int mapRadiusToUse = -1;
		for(Entry<Integer, RadiusBasedCoinFeature> entry:radiusFeatureMap.entrySet()){
			int mapRadius = entry.getKey();
			if ( squaredDistanceBelongsToMapRadius(squaredDistanceFromCenter, mapRadius) ){
				mapRadiusToUse = mapRadius;
				break;
			}
		}
		return radiusFeatureMap.get(mapRadiusToUse);
	}

	private boolean squaredDistanceBelongsToMapRadius(int squaredDistanceFromCenter, int mapRadius) {
		return (squaredDistanceFromCenter >= ((mapRadius-1)*(mapRadius-1)) + mapRadius) &&
				(squaredDistanceFromCenter <= (mapRadius*mapRadius) + mapRadius);
	}
	
}
